package com.qdsgjsfk.vision.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.qdsgjsfk.vision.R;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.CheckResponse;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsgjsfk.vision.base.BaseFragment;

import java.util.Objects;

import util.PreferencesHelper;

/**
 * Created by HP on 2018/10/8.
 */

public class OtherFragment extends BaseFragment {

    @BindView(R.id.odSph)
    TextView odSph;

    @BindView(R.id.odCyl)
    TextView odCyl;

    @BindView(R.id.odAxial)
    TextView odAxial;

    @BindView(R.id.osSph)
    TextView osSph;

    @BindView(R.id.osCyl)
    TextView osCyl;

    @BindView(R.id.osAxial)
    TextView osAxial;

    @BindView(R.id.osVision)
    TextView osVision;
    @BindView(R.id.odVision)
    TextView odVision;
    @BindView(R.id.osEsysight)
    TextView osEsysight;
    @BindView(R.id.odEyesight)
    TextView odEyesight;

    @BindView(R.id.checkbox_sejue)
    CheckBox checkbox_sejue;
    @BindView(R.id.checkbox_lts)
    CheckBox checkbox_lts;
    @BindView(R.id.checkbox_wxq)
    CheckBox checkbox_wxq;
    @BindView(R.id.checkbox_zs)
    CheckBox checkbox_zs;
    @BindView(R.id.checkbox_js)
    CheckBox checkbox_js;
    @BindView(R.id.checkbox_ys)
    CheckBox checkbox_ys;
    @BindView(R.id.checkbox_sg)
    CheckBox checkbox_sg;
    @BindView(R.id.checkbox_rs)
    CheckBox checkbox_rs;
    @BindView(R.id.checkbox_xs)
    CheckBox checkbox_xs;
    @BindView(R.id.checkbox_edt_cbzd_qt)
    CheckBox checkbox_edt_cbzd_qt;
    @BindView(R.id.checkbox_jkfc)
    CheckBox checkbox_jkfc;
    @BindView(R.id.checkbox_sgynfc)
    CheckBox checkbox_sgynfc;
    @BindView(R.id.checkbox_bnfc)
    CheckBox checkbox_bnfc;
    @BindView(R.id.checkbox_ysjy_qt)
    CheckBox checkbox_ysjy_qt;
    @BindView(R.id.checkbox_sgmz)
    CheckBox checkbox_sgmz;
    @BindView(R.id.checkbox_xrszk)
    CheckBox checkbox_xrszk;
    @BindView(R.id.checkbox_ptykmz)
    CheckBox checkbox_ptykmz;
    @BindView(R.id.checkbox_fczk_qt)
    CheckBox checkbox_fczk_qt;

    @BindView(R.id.edt_sejue)
    EditText edt_sejue;
    @BindView(R.id.edt_lts)
    EditText edt_lts;
    @BindView(R.id.edt_cbzd_qt)
    EditText edt_cbzd_qt;
    @BindView(R.id.edt_ysjy_qt)
    EditText edt_ysjy_qt;
    @BindView(R.id.edt_fczk_qt)
    EditText edt_fczk_qt;


    @BindView(R.id.btn_confirm)
    TextView btn_confirm;

    long studentId, id, id1, id2;
    String taskId;

    public static OtherFragment newInstance(long studentId , String taskId) {
        Bundle args = new Bundle();
        args.putLong("studentId", studentId);
        args.putString("taskId",taskId);
        OtherFragment fragment = new OtherFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_else;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        assert getArguments() != null;
        studentId = getArguments().getLong("studentId");
        taskId = getArguments().getString("taskId");
//        getInfo();
//        getInfo2();
    }


//    private void getInfo() {
//        RestProxy.getInstance().getStudentSecondInfo(studentId + "", PreferencesHelper.getInstance().getString(getActivity(),"token"),new Observer<StudentInfoResponse>() {
//            @Override
//            public void onSubscribe(Disposable d) {
//
//            }
//
//            @Override
//            public void onNext(StudentInfoResponse studentInfoResponse) {
//                odSph.setText(studentInfoResponse.data.odSph);
//                odCyl.setText(studentInfoResponse.data.odCyl);
//                odAxial.setText(studentInfoResponse.data.odAxial);
//                osSph.setText(studentInfoResponse.data.osSph);
//                osCyl.setText(studentInfoResponse.data.osCyl);
//                osAxial.setText(studentInfoResponse.data.osAxial);
//                id1 = studentInfoResponse.data.id;
//                getCheck();
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                e.printStackTrace();
//            }
//
//            @Override
//            public void onComplete() {
//
//            }
//        });
//    }


//    private void getInfo2() {
//        RestProxy.getInstance().getStudentFirstInfo(studentId + "", PreferencesHelper.getInstance().getString(getActivity(),"token"), new Observer<StudentInfoResponse>() {
//            @Override
//            public void onSubscribe(Disposable d) {
//
//            }
//
//            @Override
//            public void onNext(StudentInfoResponse studentInfoResponse) {
//                osVision.setText(studentInfoResponse.data.osVision);
//                odVision.setText(studentInfoResponse.data.odVision);
//                osEsysight.setText(studentInfoResponse.data.osEyesight);
//                odEyesight.setText(studentInfoResponse.data.odEyesight);
//                id2 = studentInfoResponse.data.id;
//                getCheck();
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                e.printStackTrace();
//            }
//
//            @Override
//            public void onComplete() {
//
//            }
//        });
//    }

    @OnClick(R.id.btn_confirm)
    void btn_confirm() {
        saveCheck();
    }

    private void saveCheck() {
        if (id1 == 0 || id2 == 0) {
            return;
        }
        String other = getCheckBoxStatus(checkbox_sejue) + "," + getCheckBoxStatus(checkbox_lts) + "," + getCheckBoxStatus(checkbox_wxq) + "," + edt_sejue.getText().toString() + "," + edt_lts.getText().toString();
        String diagnoseInit = getCheckBoxStatus(checkbox_zs) + "," + getCheckBoxStatus(checkbox_js) + "," + getCheckBoxStatus(checkbox_ys) + "," + getCheckBoxStatus(checkbox_sg) + "," + getCheckBoxStatus(checkbox_rs) + "," + getCheckBoxStatus(checkbox_xs) + "," + getCheckBoxStatus(checkbox_edt_cbzd_qt) + "," + edt_cbzd_qt.getText().toString();
        String docAdvice = getCheckBoxStatus(checkbox_jkfc) + "," + getCheckBoxStatus(checkbox_sgynfc) + "," + getCheckBoxStatus(checkbox_bnfc) + "," + getCheckBoxStatus(checkbox_ysjy_qt) + "," + edt_ysjy_qt.getText().toString();
        String reviewDepart = getCheckBoxStatus(checkbox_sgmz) + "," + getCheckBoxStatus(checkbox_xrszk) + "," + getCheckBoxStatus(checkbox_ptykmz) + "," + getCheckBoxStatus(checkbox_fczk_qt) + "," + edt_fczk_qt.getText().toString();


        RestProxy.getInstance().saveCheck(id1 + "", id2 + "", diagnoseInit, docAdvice, reviewDepart, other, id + "", PreferencesHelper.getInstance().getString(getActivity(),"token"), new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {
                btn_confirm.setClickable(false);
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
                Objects.requireNonNull( OtherFragment.this.getActivity() ).finish();
            }

            @Override
            public void onError(Throwable e) {
                Toast.makeText(mContext, "系统繁忙，请重试", Toast.LENGTH_LONG).show();
                btn_confirm.setClickable(true);
            }

            @Override
            public void onComplete() {

            }
        });
    }


    private int getCheckBoxStatus(CheckBox cb) {
        if (cb.isChecked())
            return 1;
        else
            return 0;
    }


    private void setViweStatus(String s, View cb) {
        if (cb instanceof CheckBox) {
            if (s.equals("1")) {
                ((CheckBox) cb).setChecked(true);
            } else {
                ((CheckBox) cb).setChecked(false);
            }
        } else if (cb instanceof EditText) {
            ((EditText) cb).setText(s);
        }
    }


    private void getCheck() {
        RestProxy.getInstance().getCheck(id1 + "", id2 + "", PreferencesHelper.getInstance().getString(getActivity(),"token"),new Observer<CheckResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CheckResponse checkResponse) {
                String diagnoseInit[] = checkResponse.data.diagnoseInit.split(",");
                setViweStatus(diagnoseInit[0], checkbox_zs);
                setViweStatus(diagnoseInit[1], checkbox_js);
                setViweStatus(diagnoseInit[2], checkbox_ys);
                setViweStatus(diagnoseInit[3], checkbox_sg);
                setViweStatus(diagnoseInit[4], checkbox_rs);
                setViweStatus(diagnoseInit[5], checkbox_xs);
                setViweStatus(diagnoseInit[6], checkbox_edt_cbzd_qt);
                if (diagnoseInit.length > 7)
                    setViweStatus(diagnoseInit[7], edt_cbzd_qt);

                String docAdvice[] = checkResponse.data.docAdvice.split(",");
                setViweStatus(docAdvice[0], checkbox_jkfc);
                setViweStatus(docAdvice[1], checkbox_sgynfc);
                setViweStatus(docAdvice[2], checkbox_bnfc);
                setViweStatus(docAdvice[3], checkbox_ysjy_qt);
                if (docAdvice.length > 4)
                    setViweStatus(docAdvice[4], edt_ysjy_qt);


                String reviewDepart[] = checkResponse.data.reviewDepart.split(",");
                setViweStatus(reviewDepart[0], checkbox_sgmz);
                setViweStatus(reviewDepart[1], checkbox_xrszk);
                setViweStatus(reviewDepart[2], checkbox_ptykmz);
                setViweStatus(reviewDepart[3], checkbox_fczk_qt);
                if (reviewDepart.length > 4)
                    setViweStatus(reviewDepart[4], edt_fczk_qt);


                String other[] = checkResponse.data.other.split(",");
                setViweStatus(other[0], checkbox_sejue);
                setViweStatus(other[1], checkbox_lts);
                setViweStatus(other[2], checkbox_wxq);
                if (other.length > 3)
                    setViweStatus(other[3], edt_sejue);
                if (other.length > 4)
                    setViweStatus(other[4], edt_lts);

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    @OnClick(R.id.checkbox_jkfc)
    void checkbox_jkfc() {
        checkbox_sgynfc.setChecked(false);
        checkbox_bnfc.setChecked(false);
        checkbox_ysjy_qt.setChecked(false);
    }


    @OnClick(R.id.checkbox_bnfc)
    void checkbox_bnfc() {
        checkbox_sgynfc.setChecked(false);
        checkbox_jkfc.setChecked(false);
        checkbox_ysjy_qt.setChecked(false);
    }

    @OnClick(R.id.checkbox_sgynfc)
    void checkbox_sgynfc() {
        checkbox_jkfc.setChecked(false);
        checkbox_bnfc.setChecked(false);
        checkbox_ysjy_qt.setChecked(false);
    }

    @OnClick(R.id.checkbox_ysjy_qt)
    void checkbox_ysjy_qt() {
        checkbox_sgynfc.setChecked(false);
        checkbox_bnfc.setChecked(false);
        checkbox_jkfc.setChecked(false);
    }


    @OnClick(R.id.checkbox_sgmz)
    void checkbox_sgmz() {
        checkbox_xrszk.setChecked(false);
        checkbox_ptykmz.setChecked(false);
        checkbox_fczk_qt.setChecked(false);
    }


    @OnClick(R.id.checkbox_xrszk)
    void checkbox_xrszk() {
        checkbox_sgmz.setChecked(false);
        checkbox_ptykmz.setChecked(false);
        checkbox_fczk_qt.setChecked(false);
    }

    @OnClick(R.id.checkbox_ptykmz)
    void checkbox_ptykmz() {
        checkbox_sgmz.setChecked(false);
        checkbox_xrszk.setChecked(false);
        checkbox_fczk_qt.setChecked(false);
    }

    @OnClick(R.id.checkbox_fczk_qt)
    void checkbox_fczk_qt() {
        checkbox_sgmz.setChecked(false);
        checkbox_xrszk.setChecked(false);
        checkbox_ptykmz.setChecked(false);
    }


}
